var grid_url;
$(document).ready(function(){
	grid_url = $("#gridform").attr("action");
	if(grid_url!=undefined && grid_url!=''){
		initMain();
	}
});
function initMain(){
	$(".datepicker").datepicker({dateFormat:'yy-mm-dd'});
	//alert(grid_url);
	$("#grid").load(grid_url,function(){
			//buildPageToolbar();
	});
}

function selects(e){
	if($(e).attr("checked")==true){
		$("#gridform").find(":checkbox").attr("checked",true);
	}else{
		$("#gridform").find(":checkbox").attr("checked",false);
	}
	
}

function jumpPage(pageNo, totalPages) {
	if(totalPages!=null){
		if(new Number(pageNo) > new Number(totalPages)){
			alert("pageNo too da");
			return;
		}
	}
	$("#pageNo").attr("value",pageNo);
	$.post(grid_url, $("#gridform").serialize(),function(data){
		$("#grid").html(data);
	});
}
function refresh(){
	jumpPage($("#pageNo").val());
}
function reload(){
	document.getElementById("gridform").reset();
	window.location.reload();
	
}
var desc = "asc";
function orderBy(orderBy) {
	//有注入漏动
	$("#pageOrder").val(orderBy+" "+desc);
	if(desc == "desc"){
		desc = "asc";
	}else{
		desc = "desc";
	}
	refresh();
}

function search() {
	jumpPage(1);
}

function remove(url,msg){
	var is = false;
	$("#gridform").find(":checkbox").each(function(){
	    if($(this).attr("name")=="ids"){
	    	if($(this).attr("checked")==true){
	    		is=true;
	    	}
	    }
	});
	if(!is){
		alert("请最少选择一项");
		return;
	}
	
	if(confirm(msg) == true){
		if(url.indexOf("/")!=-1){
			url = url.substring(url.indexOf("/")+1,url.length);
		}
		$.post(url, $("#gridform").serialize(),function(date){
			try{
				var result =eval("("+date+")");//转换为json对象
			}catch(err){
			   alert("构造JSON格式错误\n\n"+date);
			}
           	if(result.type=="error"){
           		alert("出错："+result.message);
           	}
           	
           	if(result.type=="success"){
           		refresh();
           		setMsg(result.message);
           	}
		});
	}
}
